/**
 *All rights reserved. This material is confidential and proprietary to Tungsten.
 */
package tungsten.t.database.testcase.inf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import tungsten.t.database.DBBaseHandler;
import tungsten.t.util.ClassUtil;

/**
 * 
 * @author tracy
 * @date 2013-9-3
 * @version Uno
 */
public class DBTest
{

    private static Map<Integer, TestCase> testCaseMap;

    private static Scanner scanner;

    public static void main(String[] args)
    {

        if (!DBBaseHandler.init("../Lib/config/db.properties"))
        {
            System.err.println("加载DB配置文件失败");
            return;
        }
        testCaseMap = new HashMap<Integer, TestCase>();
        scanner = new Scanner(System.in);
        try
        {

            int counter = 0;
            for (Class<?> claz : ClassUtil.getClasses("tungsten.t.database.testcase.inf.impl",
                                                      true))
            {
                testCaseMap.put(++counter, (TestCase) claz.newInstance());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        testCase();
    }

    private static void testCase()
    {
        StringBuffer sb = new StringBuffer("请选择需要执行的测试类 :\n");
        for (Integer num : testCaseMap.keySet())
        {
            sb.append(" ");
            sb.append(num);
            sb.append(" : ");
            sb.append(testCaseMap.get(num).getDesc());
            sb.append("\n");
        }
        sb.append("-1 : 结束\n");
        System.out.println(sb.toString());
        int testCaseNum = scanner.nextInt();

        if (testCaseNum == -1)
        {
            System.out.println();
            System.out.println("***********");
            System.out.println("* 测试结束  *");
            System.out.println("***********");
            return;
        }
        else
        {
            testMethod(testCaseMap.get(testCaseNum));
        }
    }

    private static void testMethod(TestCase testCase)
    {
        System.out.printf("\n\t【%s】选择测试方法 ：\n", testCase.getDesc());
        System.out.printf(testCase.listTestMethods().toString());
        System.out.printf("\t-1 : 返回\n\n");

        int methodNum = scanner.nextInt();

        if (methodNum == -1)
        {
            testCase();
        }
        else
        {
            Collection<String> params = new ArrayList<String>();
            System.out.println("\n" + testCase.listMethodParams(methodNum));
            String input = scanner.next();
            boolean isBack = false; // 防止递归调用过多方法堆
            while (!"#".equals(input))
            {
                if ("$".equals(input))
                {
                    isBack = true;
                }
                if (isBack)
                {
                    break;
                }
                params.add(input);
                input = scanner.next();
            }
            if (isBack)
            {
                testMethod(testCase); // 尾递归
            }
            else
            {
                testCase.testExecute(methodNum,
                                     (String[]) params.toArray(new String[params.size()]));
                System.out.println();
                System.out.println("***********");
                System.out.println("* 测试结束  *");
                System.out.println("***********");
            }
        }
    }

}
